package com.yb.project.system.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yb.common.domain.AjaxObject;
import com.yb.common.domain.PageResult;
import com.yb.common.utils.StringUtil;
import com.yb.framework.log.annotation.Log;
import com.yb.framework.log.enums.OperateTypeEnum;
import com.yb.project.base.controller.BaseController;
import com.yb.project.system.domain.SysConfig;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Controller
@RequestMapping("/system/config")
@Api(tags = "配置管理")
public class SysConfigController extends BaseController {
	
	Logger logger = LoggerFactory.getLogger(SysConfigController.class.getName());
	
	private static String SYS_CONFIG = "system/config/config";
	private static String SYS_CONFIG_ADD = "system/config/config_add";
	private static String SYS_CONFIG_EDIT = "system/config/config_edit";
	
	@GetMapping()
	@PreAuthorize("hasAuthority('system:config')")
	public String main() {
		return SYS_CONFIG;
	}
	
	@GetMapping("/add_view")
	@PreAuthorize("hasAuthority('system:config:add')")
	public String addView() {
		return SYS_CONFIG_ADD;
	}
	
	@GetMapping("/edit_view/{id}")
	@PreAuthorize("hasAuthority('system:config:edit')")
	public String editView(@PathVariable("id") Long id, ModelMap model) {
		model.put("sysConfig", sysConfigService.getById(id));
		return SYS_CONFIG_EDIT;
	}
	
	/**
	 * 配置列表
	 */
	@PostMapping("/list")
	@PreAuthorize("hasAuthority('system:config:list')")
	@ApiOperation(value="分页查询", notes="分页查询配置信息")
	public @ResponseBody PageResult list(SysConfig sysConfig) {
		startPage();
		return getPageInfo(sysConfigService.getPageByEntity(sysConfig));
	}
	
	/**
	 * 新增配置
	 */
	@PostMapping("/add")
	@PreAuthorize("hasAuthority('system:config:add')")
	@ApiOperation(value="新增配置", notes="新增配置信息")
	@Log(label = "新增配置", operateType = OperateTypeEnum.ADD)
	public @ResponseBody AjaxObject add(SysConfig sysConfig) {
		return toAjax(sysConfigService.insert(sysConfig), "新增配置" + "[" + sysConfig.getName() + "]");
	}
	
	/**
	 * 编辑配置
	 */
	@PostMapping("/edit")
	@PreAuthorize("hasAuthority('system:config:edit')")
	@ApiOperation(value="编辑配置", notes="编辑配置信息")
	@Log(label = "编辑配置", operateType = OperateTypeEnum.EDIT)
	public @ResponseBody AjaxObject edit(SysConfig sysConfig) {
		return toAjax(sysConfigService.update(sysConfig), "编辑配置" + "[" + sysConfig.getName() + "]");
	}
	
	/**
	 * 删除配置
	 */
	@PostMapping("/delete")
	@PreAuthorize("hasAuthority('system:config:delete')")
	@ApiOperation(value="删除配置", notes="批量删除配置信息")
	@Log(label = "删除配置", operateType = OperateTypeEnum.DELETE)
	public @ResponseBody AjaxObject delete(@RequestParam("ids") String ids) {
		List<String> sysConfigNames = sysConfigService.getNamesByIds(StringUtil.str2LongArray(ids));
		return toAjax(sysConfigService.deleteListByIds(StringUtil.str2LongArray(ids)), "批量删除配置" + sysConfigNames);
	}
}